/**
 * 问答管理
 */
var pageCurr;
var form;
$(function() {
    layui.use(['table','util'], function(){
        var table = layui.table;
        form = layui.form;
        var util = layui.util;
        tableIns=table.render({
            id:'id',
            elem: '#answerQuestionsList',
            url:'/back/answerQuestions/answerQuestionsList',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {type:'numbers'/*,width:"5%"*/}
                ,{field:'content', title:'问题内容',align:'center'/*,width:"10%"*/}
                ,{field:'status', title:'状态',align:'center'/*,width:"15%"*/}
                ,{field: 'createDate', title: '创建时间', templet: function (d) {
                        var createDate = util.toDateString(new Date(d.createDate).getTime(), "yyyy-MM-dd HH:mm:ss");
                        return '<div>' + createDate + '</div>'
                    }, align: 'center'}
                , {field: 'updateDate', title: '更新时间', templet: function (d) {
                        var updateDate = util.toDateString(new Date(d.updateDate).getTime(), "yyyy-MM-dd HH:mm:ss");
                        return '<div>' + updateDate + '</div>'
                    }, align: 'center'}
                ,{fixed:'right',title:'操作',align:'center', toolbar:'#optBar'/*,width:"25%"*/}
            ]],
        done: function(res, curr, count){
                $("[data-field='status']").children().each(function(){
                    if($(this).text()=='1'){
                        $(this).text("发布");
                    }else if ($(this).text()=='2') {
                        $(this).text("草稿");
                    }
                });
                pageCurr=curr;

            }
        });


        //监听工具条
        table.on('tool(answerQuestionsTable)', function(obj){
            var data = obj.data;
            console.log(JSON.stringify(data)+"======")
            if(obj.event === 'del'){
                //删除
                del(data,data.id);
            } else if(obj.event === 'edit'){
                //编辑
                edit(data);
            }
        });

    });

});

//提交表单
function formSubmit(obj){
    $.ajax({
        type: "post",
        data: $("#permissionForm").serialize(),
        url: "/back/answerQuestions/setAnswerQuestions",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                load(obj);
            });
        }
    });
}
//新增
function add() {
    edit(null,"新增");
}
//打开编辑框
function edit(data,title){
    var id;
    if(data != null){
        id = data.id;
    }
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);

    layer.open({
        type: 2,
        closeBtn: 1,
        title: '修改问答内容',
        area: ['450px','540px'],
        content:'/back/answer_questions_operation?id=' + id,
        end:function(){

        }
    });
}

//重新加载table
function load(obj){
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

//删除
function del(obj,id) {
    if(null!=id){
        layer.confirm('您确定要删除吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/back/answerQuestions/del",{"id":id},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}